package com.tarena.lbs.entity;

import lombok.Data;

import java.io.Serializable;
import java.util.Date;

@Data
public class Merchant implements Serializable {

    private static final long serialVersionUID = 1L;

    private Long id;

    private String name;

    private String address;

    private Double latitude;

    private Double longitude;

    private String phone;

    private String businessHours;

    private Integer status;

    private Date createTime;

    private Date updateTime;

    // 计算与用户的距离
    public double calculateDistance(double userLat, double userLng) {
        // 使用Haversine公式计算两点间距离
        double radLat1 = Math.toRadians(userLat);
        double radLat2 = Math.toRadians(this.latitude);
        double a = radLat1 - radLat2;
        double b = Math.toRadians(userLng) - Math.toRadians(this.longitude);
        double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a/2), 2) +
                Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b/2), 2)));
        s = s * 6378.137; // 地球半径，单位千米
        s = Math.round(s * 10000) / 10000; // 保留4位小数
        return s;
    }
}
